package com.army.study.utils;

import com.army.study.ResourceTable;
import com.army.study.databean.Item;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static ohos.data.search.schema.PhotoItem.TAG;

public class Utils {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0xD001400, "Utils");

    private static final List<Integer> PICTURE_IDS = Arrays.asList(ResourceTable.Media_1,
            ResourceTable.Media_2, ResourceTable.Media_3,
            ResourceTable.Media_4, ResourceTable.Media_5,
            ResourceTable.Media_6, ResourceTable.Media_7,
            ResourceTable.Media_8, ResourceTable.Media_9,
            ResourceTable.Media_10);


    private static List<PixelMap> resourcePixelMaps;


    public static void transResourceIdsToListOnce(Context context) {
        resourcePixelMaps = new ArrayList<>(0);
        // Set the pixel map
        for (int index: PICTURE_IDS) {
            InputStream source = null;
            ImageSource imageSource;
            try {
                source = context.getResourceManager().getResource(index);
                imageSource = ImageSource.create(source, null);
                resourcePixelMaps.add(imageSource.createPixelmap(null));
            } catch (IOException | NotExistException e) {
                HiLog.error(TAG, "Get Resource PixelMap error");
            } finally {
                try {
                    assert source != null;
                    source.close();
                } catch (IOException e) {
                    HiLog.error(TAG, "getPixelMap source close error");
                }
            }
        }
    }

    /**
     * 封装列表项数据
     * @return
     */
    public static List<Item> getItems() {
        List<Item> items = new ArrayList<>();

        int index = 1;
        for (PixelMap pixelMap : resourcePixelMaps) {
            Item item = new Item();
            item.setImg(pixelMap);
            item.setTitle("测试标题 " + index);
            items.add(item);
            index++;
        }

        return items;
    }

    /**
     * 根据下标获取列表项数据
     * @param index
     * @return
     */
    public static Item getItem(int index) {
        List<Item> items = getItems();
        return items.get(index);
    }

    /**
     * 创建提示框
     * @param context
     * @param message
     */
    public static void createToastDialog(Context context, String message) {
        Component component = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_toast, null, false);
        Component componentText = component.findComponentById(ResourceTable.Id_msg_toast);
        if (componentText instanceof Text) {
            ((Text) componentText).setText(message);
        }
        new ToastDialog(context)
                .setComponent(component)
                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                .setAlignment(LayoutAlignment.CENTER).show();
    }
}
